package com.bawei.mine


import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.base.RoutePath
import com.bawei.common.base.BaseActivity
import com.bawei.mine.adapter.GoodsOrdersAdapter
import com.bawei.mine.databinding.ActivityMineBinding
import com.bawei.mine.entity.Data
import com.bawei.mine.intent.MineViewIntent
import com.bawei.mine.state.MineUIState
import com.bawei.mine.vm.MineViewModel
import com.google.android.material.tabs.TabLayout
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = RoutePath.PATH_MINE_PAGE)
class MineActivity : BaseActivity<ActivityMineBinding>() {

    private val mViewModel :MineViewModel by viewModels()
    private lateinit var goodsAdapter :GoodsOrdersAdapter
private val list = mutableListOf<Data>()
    override fun getLayoutId(): Int = R.layout.activity_mine

    fun sendHttpApi(one :Int ,two :Int ,three :Int){
        lifecycleScope.launch {
            mViewModel.channel.send(MineViewIntent.loadGoodsAllOrder(one,two,three))
        }
    }
    override fun initViews() {

        sendHttpApi(0,1,30)
        goodsAdapter = GoodsOrdersAdapter(list)
        mDataBinding.recyclerView.layoutManager=LinearLayoutManager(this@MineActivity ,LinearLayoutManager.VERTICAL,false)
        mDataBinding.recyclerView.adapter =goodsAdapter

        lifecycleScope.launch {
            mViewModel.uiState.collect{
            when(it){
                is MineUIState.LoadOrderAllSuccess ->{
                    list.clear()
                    list.addAll(it.data)
                    goodsAdapter.notifyDataSetChanged()
                }
                is MineUIState.LoadOrderAllFailed -> showToastMsg(it.msg)

                else->{

                }
            }
            }
        }



mDataBinding.tabLayout.setOnTabSelectedListener(object  :TabLayout.OnTabSelectedListener{
    override fun onTabSelected(tab: TabLayout.Tab?) {
               when(tab?.position){
                   0->{
                       MMKV.defaultMMKV().putString("orderState","全部订单")
                       sendHttpApi(0,1,30)
                       goodsAdapter.notifyDataSetChanged()
                   }
                   1->{
                       sendHttpApi(15,1,2)
                       MMKV.defaultMMKV().putString("orderState","待支付")
                       goodsAdapter.notifyDataSetChanged()
                   }

                   2->{
                       sendHttpApi(24,1,4)
                       MMKV.defaultMMKV().putString("orderState","待发货")
                       goodsAdapter.notifyDataSetChanged()
                   }
                   3->{
                       sendHttpApi(14,1,1)
                       MMKV.defaultMMKV().putString("orderState","待收货")
                       goodsAdapter.notifyDataSetChanged()
                   }
                   4->{
                       sendHttpApi(0,1,30)
                       goodsAdapter.notifyDataSetChanged()
                       MMKV.defaultMMKV().putString("orderState","已完成")
                   }
               }

    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {

    }

    override fun onTabReselected(tab: TabLayout.Tab?) {

    }

})


    }

}